<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
    Class: Auth

    Author: 
        Lonnie Ezell
*/
class khvt_report{
    private $ci;
    
    public function __construct($config = array())
    {
        $this->ci =& get_instance();
        //log_message('debug', "Email Class Initialized");
        if (!class_exists('vt_model')){
            $this->ci->load->model('vt_model');
        }
        //$user_login = $this->ci->user_model->getUserByName($username);
    }
    
    function render_report($report_code){        
        //$report_code = date("d-m-Y");
        //$this->vt_model->unique_report_code($report_code);
        $report = $this->ci->vt_model->getReportByCode($report_code);
        if($report == false){
            $product_data = $this->ci->vt_model->getALLProductToReport();
            
            $report_data = array(
                'code' => $report_code,
                'file_name' => $report_code,
                'created_date' => $report_code.' 00:00:00',
                'data' => json_encode($product_data),
                'bill_import' => null,
                'bill_export' => null
            );
            $report_id = $this->ci->vt_model->insertReport($report_data);
            return true;
        }else{
            if($report->data == null){
                $product_data = $this->ci->vt_model->getALLProductToReport();
                $this->ci->vt_model->updateReport(array(
                    'data' => json_encode($product_data)
                ),$report->id);
            }
        }
        
        return false;
    }
    
    function update_report($report_code){
        $this->render_report($report_code);
        $products_default = $this->ci->vt_model->getALLProductToReport();
        $report = $this->ci->vt_model->getReportByCode($report_code);
        
        $products_report = json_decode($report->data,true);
        //$this->vt_model->unique_report_code($report_code);

        $import = $this->ci->vt_model->getALLProductImportByDate($report_code);
        $export = $this->ci->vt_model->getALLProductExportByDate($report_code);
        $revert = $this->ci->vt_model->getALLProductRevertByDate($report_code);
        
        foreach($products_default As $pro_id => $pro){
            if( isset($products_report[$pro_id]) ){
                $products_default[$pro_id] = $products_report[$pro_id];
            }
            if( isset($import[$pro_id]) ){
                $products_default[$pro_id]['it'] = decimal_format($import[$pro_id]['count']);
            }
            if( isset($export[$pro_id]) ){
                $products_default[$pro_id]['et'] = decimal_format($export[$pro_id]['count']);
            }
            if( isset($revert[$pro_id]) ){
                $products_default[$pro_id]['rt'] = decimal_format($revert[$pro_id]['count']);
            }
        }
        
        $import_bill = $this->ci->vt_model->getImportBillByDate($report_code);
        $imb_data = array();
        if($import_bill != false){
            foreach($import_bill As $imb){
                $imb_data[] = array(
                    $imb->id,//0
                    $imb->bill_code,//1
                    $imb->username,//2
                    $imb->supplier_name,//3
                    decimal_format($imb->bill_total),//4
                    decimal_format($imb->bill_vat),//5
                    decimal_format($imb->bill_vatprice),//6
                    decimal_format($imb->bill_totalvat)//7
                );
            }
        }
        
        $export_bill = $this->ci->vt_model->getExportBillByDate($report_code);
        $exp_data = array();
        if($export_bill != false){
            foreach($export_bill As $exp){
                $exp_data[] = array(
                    $exp->id,//0
                    $exp->bill_code,//1
                    $exp->username,//2
                    $exp->name,//3
                    decimal_format($exp->bill_total),//4
                    decimal_format($exp->bill_vat),//5
                    decimal_format($exp->bill_vatprice),//6
                    decimal_format($exp->bill_totalvat)//7
                );
            }
        }
        
        $revert_product = $this->ci->vt_model->getALLProductRevertByDate($report_code);
        $revert_data = array();
        foreach($revert_product As $rev){
            $revert_data[$rev['product_id']] = array(
                $rev['product_id'],//0
                $rev['code'],//1
                decimal_format($rev['count']),//2
                decimal_format($rev['price'])//3
            );
        }
        
        $this->ci->vt_model->updateReport(array(
            'data' => ( count($imb_data) > 0 ) ? json_encode($products_default) : null,
            'bill_import' => ( count($imb_data) > 0 ) ? json_encode($imb_data) : null,
            'bill_export' => ( count($exp_data) > 0 ) ? json_encode($exp_data) : null,
            'revert' => ( count($revert_data) > 0 ) ? json_encode($revert_data) : null
        ),$report->id);
    }  
}